#include "languagecontrol.h"

LanguageControl::LanguageControl(QQmlEngine *engine)
    : m_Engine(engine)
{
    m_Translator = new QTranslator(this);

    QCoreApplication::installTranslator(m_Translator);
}

void LanguageControl::load_Language(int index)
{
    switch (index) {
    case 1: {
        qDebug() << "加载 中文";
        if (m_Translator->load(QCoreApplication::applicationDirPath() + "/Dictionary/zh_CN.qm")) {
            qDebug() << "加载成功 - zh_CN";
        }
        break;
    }
    case 2: {
        qDebug() << "加载 英文";
        if (m_Translator->load(QCoreApplication::applicationDirPath() + "/Dictionary/en_US.qm")) {
            qDebug() << "加载成功 - en_US";
        }
        break;
    }
    case 3: {
        qDebug() << "加载 越南文";
        if (m_Translator->load(QCoreApplication::applicationDirPath() + "/Dictionary/vi_VN.qm")) {
            qDebug() << "加载成功 - vi_VN";
        }
        break;
    }
    default: {
        qDebug() << "加载 中文";
        if (m_Translator->load(QCoreApplication::applicationDirPath() + "/Translation/zh_CN.qm")) {
            qDebug() << "加载成功 - zh_CN";
        }
        break;
    }
    }

    m_Engine->retranslate();
}

int LanguageControl::CurrentLanguage() const
{
    return m_CurrentLanguage;
}

void LanguageControl::setCurrentLanguage(int newCurrentLanguage)
{
    if (m_CurrentLanguage == newCurrentLanguage)
        return;
    m_CurrentLanguage = newCurrentLanguage;
    emit CurrentLanguageChanged();
}
